package xin.marcher.module.rights.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.rights.api.RoleApi;
import xin.marcher.module.rights.domain.dto.RoleDTO;
import xin.marcher.module.rights.domain.request.QueryRoleRequest;
import xin.marcher.module.rights.domain.request.RemoveRoleRequest;
import xin.marcher.module.rights.domain.request.RoleRequest;
import xin.marcher.module.rights.service.RoleService;

/**
 *
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = RoleApi.class, retries = 0)
public class RoleApiImpl implements RoleApi {

    @Autowired
    private RoleService roleService;

    @Override
    public JsonResult saveRole(RoleRequest request) {
        try {
            return roleService.saveRole(request);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult removeRole(RemoveRoleRequest request) {
        try {
            return roleService.removeRole(request);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult queryRole(QueryRoleRequest request) {
        try {
            PageResult<RoleDTO> pageResult = roleService.queryRole(request);
            return JsonResult.buildSuccess(pageResult);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

}
